package com.sfeir.fastcall.access;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeMap;

import android.database.Cursor;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;

import com.sfeir.fastcall.model.PhoneEntry;

/**
 * Cette classe permet de recuperer les donnees relative au contact, depuis le repertoire du smartphone.
 * 
 * Created by :
 * 
 * 		@author SFEIR
 */
public class ContactProviderSdk3 extends ContactProviderCommons {

	protected void loadFromContacts(final TreeMap<String, TreeMap<String, HashSet<Integer>>> indexContactLetterTmp) {
		final Cursor cur = cr.query(People.CONTENT_URI, 
				new String[] {People._ID, People.DISPLAY_NAME},
				People.NUMBER +" is not null", 
				null,
				null);
		if (cur.getCount() > 0) {
			String displayName, token;
			String[] displayNameTokens;

			while (cur.moveToNext()) {
				displayName = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));

				displayNameTokens = displayName.split("\\s");
				for (int i = 0; i < displayNameTokens.length; i++) {
					token = displayNameTokens[i].toUpperCase();
					while(token.length()<2){
						token = token.concat(" ");
					}
					TreeMap<String, HashSet<Integer>> firstLevel = indexContactLetterTmp.get(token.substring(0, 1));
					if(firstLevel == null){
						firstLevel = new TreeMap<String,HashSet<Integer>>();
						indexContactLetterTmp.put(token.substring(0, 1), firstLevel);
					} 
					HashSet<Integer> contactIds = firstLevel.get(token.substring(0, 2));
					if(contactIds == null){
						contactIds = new HashSet<Integer>();
						firstLevel.put(token.substring(0, 2), contactIds);
					}
					contactIds.add(cur.getInt(cur.getColumnIndex(People._ID)));

				}
			}
		}
		cur.close();
	}
	
	public ArrayList<PhoneEntry> filtreContacts(String filtre) {
		final ArrayList<PhoneEntry> contactEntries = new ArrayList<PhoneEntry>();
		
		final Set<Integer> contactIds = filtreContactIds(filtre);
		final StringBuilder where = new StringBuilder();
		where.append(Phones.PERSON_ID);
		where.append(" IN ( ");
		int i=0;
		for (int contactId : contactIds) {
			where.append(contactId);
			if (i < contactIds.size() - 1)
				where.append(" , ");
			i++;
		}		
		where.append(")");
		where.trimToSize();

		final Cursor cur = this.cr.query(Phones.CONTENT_URI,
									new String[] {Phones.PERSON_ID, Phones.NUMBER, Phones.TYPE, People.DISPLAY_NAME },
									where.toString(), 
									null,
									Phones.NAME);		
		
		while(cur.moveToNext()){
			PhoneEntry phoneEntry = new PhoneEntry();
			phoneEntry.id =  cur.getInt(cur.getColumnIndex(Phones.PERSON_ID));
			phoneEntry.phoneNumber = cur.getString(cur.getColumnIndex(Phones.NUMBER));
			phoneEntry.displayName = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
			phoneEntry.isMobile = cur.getInt(cur.getColumnIndex(Phones.TYPE)) == Phones.TYPE_MOBILE ;
			contactEntries.add(phoneEntry);
		}
		
		cur.close();
		
		return contactEntries;
	}	
	
}